﻿using Direct.IDAL.Manager.Notice;
using DirectSell.Common.Page;
using DirectSell.Model.ManagerNotice;
using DirectSell.Model.Query;
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace DirectSell.WebLayer.Areas.Manager.Controllers
{
    public class NoticeController : Controller
    {
        #region 注入
        [Dependency]
        protected INoticeManagerRepository _repository { get; set; }
        #endregion

        #region 首页
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> Index(BaseCondition condition)
        {
            condition.startTime = Request["startTime"];

            condition.endTime = Request["endTime"];
          
            var list = await _repository.GetList(condition);

            if (Request.IsAjaxRequest())
                return PartialView("_AjaxSearchPost", list);

            return View(list);
        }
        #endregion

        #region 公告信息页面
        public ActionResult NotifyInfo(int id)
        {
            var info =  _repository.QueryNotice(id);
            return PartialView(info);
        }
        #endregion

        #region 修改公告页面
        /// <summary>
        /// 修改公告页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            var info = _repository.QueryNotice(id);
            return PartialView(info);
        }
        #endregion

        #region 删除公告
        /// <summary>
        /// 删除公告
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult Del(int id)
        {
            return Json(_repository.DelNotice(id));
        }
        #endregion

        #region 修改公告
        /// <summary>
        /// 修改公告
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [ValidateInput(false)]
        public JsonResult Update(NoticeReq req)
        {
            return Json(_repository.UpdateNotice(req));
        }
        #endregion

        #region 修改公告排序
        /// <summary>
        /// 修改公告排序
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sort"></param>
        public JsonResult UpdateSort(int id,int sort)
        {
            if (!_repository.UpdateSort(id, sort))
            {
                return Json("修改成功");
            }
            return Json("修改失败");
        }
        #endregion

    }
}